package com.tom.usercenter.controller;

import com.tom.usercenter.common.BaseResponse;
import com.tom.usercenter.common.ResultCodeEnum;
import com.tom.usercenter.common.ResultUtils;
import com.tom.usercenter.constant.UserConstant;
import com.tom.usercenter.exception.BusinessException;
import com.tom.usercenter.model.domain.User;
import com.tom.usercenter.model.domain.request.UserLoginRequest;
import com.tom.usercenter.model.domain.request.UserRegisterRequest;
import com.tom.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;

    /**用户注册
     *
     * @param userRegisterRequest
     * @return
     */

    @PostMapping("register")
    BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if (userRegisterRequest == null) {
            throw new BusinessException(ResultCodeEnum.PARAMS_ERROR,"参数为空");
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword, planetCode)) {
            throw new BusinessException(ResultCodeEnum.PARAMS_ERROR,"必填信息为空");
        }
        long userId = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);
        return ResultUtils.success(userId);
    }

    /**用户登录
     *
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("login")
    BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if (userLoginRequest == null) {
            throw new BusinessException(ResultCodeEnum.PARAMS_ERROR,"参数为空");
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ResultCodeEnum.PARAMS_ERROR,"必填信息为空");
        }

        User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(user);

    }
    /**
     * 查询用户
     *
     */
    @GetMapping("query")
    public BaseResponse<List<User>> queryUser(String username, HttpServletRequest request){
        //1.校验是否为管理员
        if(!checkUser(request)) {
            throw new BusinessException(ResultCodeEnum.NO_AUTH,"无权限");
        }
        //2.查询用户信息
        List<User> userList = userService.queryUser(username);
        //用户信息脱敏
        List<User> fliterlist = userList.stream().map(user -> userService.getFilterUser(user)).collect(Collectors.toList());

        return ResultUtils.success(fliterlist);
    }


    /**删除用户
     * 只有管理员才能删除
     * @param userId
     * @param request
     * @return
     */
    @DeleteMapping("delete")
    public BaseResponse<Boolean> deleteUser(long userId,HttpServletRequest request){
        //1.校验是否为管理员
        if(!checkUser(request)) {
            throw new BusinessException(ResultCodeEnum.NO_AUTH,"无权限");
        }

        //2.查询用户信息

        //校验用户名是否为空
        if(userId < 0) {
            throw new BusinessException(ResultCodeEnum.PARAMS_ERROR,"用户ID非法");
        }
        //3.返回用户信息
        boolean i = userService.removeById(userId);
        return ResultUtils.success(i);
    }

    /**
     * 用户登出
     * @param request
     * @return
     */
    @PostMapping("logout")
    public BaseResponse<Boolean> logout(HttpServletRequest request){
        if(request==null){
            throw new BusinessException(ResultCodeEnum.PARAMS_ERROR,"参数为空");
        }
        request.getSession().removeAttribute(UserConstant.USER_LOGIN_STATE);
        return ResultUtils.success(true);
    }


    //把校验用户权限代码提取出来封装一个公共方法
    public boolean checkUser(HttpServletRequest request){
        Object attribute = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        User user = (User) attribute;
        if(user == null || user.getUserRole() != UserConstant.ADMIN_ROLE) {
            return false;
        }
        return true;
    }

}
